<?php
/**
 * Created by JetBrains PhpStorm.
 * User: joedarkangel3
 * Date: 8/28/11
 * Time: 11:00 PM
 * To change this template use File | Settings | File Templates.
 */
 
class item_relation_test extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $dsn = 'mysql://root:c3603896@localhost/unit_test_yun';
        $this->load->database($dsn);
        $this->load->model('item_relation_model', 'item_relation');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    function index()
    {
        // $food_id, $food_option_id, $action valid combo 1,2,remove --- 2,36,add.
        // invalid 1,2 add (not logical action)

        // get item test
        $get_items_test = $this->item_relation->get_item_relation(49);
        $this->unit->run(count($get_items_test['add']), 2, "get items test");

        // create test
        $iv_create = $this->item_relation->create(999, 999, 'what action');
        $this->unit->run(count($iv_create['user']), 3, "invalid create attemp");

        $not_logic_action = $this->item_relation->create(1,2, 'add');

        $v_create1 = $this->item_relation->create(1, 2, 'remove');
        $this->unit->run(count($v_create1['user']), 0, "valid creation");
        $duplicate_v_create = $this->item_relation->create(1, 2, 'remove');
        $v_create2 = $this->item_relation->create(2, 36, 'add');
        $this->unit->run(count($v_create2['user']), 0, "valid creation");

        // delete test
        $this->item_relation->_delete(1,2);
        $this->item_relation->_delete(2,36);
        $delete_confirm = $this->item_relation->get_item_relation(2);
        $delete_add_confirm = $delete_confirm['add'];
        foreach($delete_add_confirm as $row){
            if($row['food_option_id'] === '36'){
                $this->unit->run(false, true, 'delete test');
                break;
            }
        }

        // do report
        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }
}
